#ifndef _DX11RENDERER_H
#define _DX11RENDERER_H
#include <d3d11.h>
#include <d3dcompiler.h>
#include <dxgi.h>
#include <vector>
#include "WICTextureLoader\WICTextureLoader.h"
#include "World.h"
#include "Window.h"
#include "TurnsDataModel.h"
#include "DataLoader.h"
#include "VertexColoredData.h"
#include "VertexTexturedData.h"
#include "ConstantBuffers.h"

class DX11Renderer {

private:
	World *world;
	std::map<TileObject*, std::vector<Coord>*> objectPositions;

	Window *window;
	HINSTANCE hInstance;
	int showWindowParam;

	bool isDebug;

	IDXGISwapChain *swapChain;
	ID3D11Device *device;
	ID3D11DeviceContext *deviceContext;
	ID3D11RenderTargetView *renderTargetView;

	ID3D11Buffer *indexColoredBuffer;
	//std::map<ID3D11Buffer*, int> indexTexturedBuffers;
	std::vector<ID3D11Buffer*> indexTexturedBuffers;
	std::vector<int> indexTexturedCounts;
	ID3D11Buffer *vertexColoredBuffer;
	std::vector<ID3D11Buffer*> vertexTexturedBuffers;

	ID3D11DepthStencilView *depthStencilView;
	ID3D11Texture2D *depthStencilBuffer;
	
	ID3D11VertexShader *vertexColoredShader;
	ID3D11VertexShader *vertexTexturedShader;
	ID3D11PixelShader *pixelColoredShader;
	ID3D11PixelShader *pixelTexturedShader;
	ID3DBlob *vertexColoredShaderBuffer;
	ID3DBlob *vertexTexturedShaderBuffer;
	ID3DBlob *pixelColoredShaderBuffer;
	ID3DBlob *pixelTexturedShaderBuffer;
	ID3D11InputLayout *vertexColoredLayout;
	ID3D11InputLayout *vertexTexturedLayout;

	int coloredVerticesCount;
	VertexColoredData *coloredVertices;

	ID3D11Buffer *constantPerSceneBuffer;
	ConstantBufferPerSceneData cbPerSceneData;
	ID3D11Buffer *constantPerObjectBuffer;
	ConstantBufferPerObjectData cbPerObjectData;

	XMMATRIX worldViewProjectionMatrix;
	XMMATRIX worldMatrix;
	XMMATRIX cameraView;
	XMMATRIX cameraProjection;

	XMVECTOR cameraPosition;
	XMVECTOR cameraTarget;
	XMVECTOR cameraUp;

	ID3D11RasterizerState* wireFrame;

	std::vector<ID3D11ShaderResourceView*> textures;
	ID3D11SamplerState *textureSamplerState;

	DataLoader *dataLoader;

	std::vector<IDXGIAdapter*> getVideoCards();

	bool initialiseDX11();
	bool initialiseScene();

	bool initWorld();
	bool initVertices();

	bool compileShaders();
	bool createVertexShaders();
	bool createPixelShaders();

	bool createIndexBuffers();
	bool createVertexBuffers();
	bool createConstantBuffers();

	bool createInputLayouts();

	void createAndSetViewport();
	void initCamera();
	bool createWireFrame();

	bool createShaderResources();

	void prepareData();

public:
	DX11Renderer(HINSTANCE, int, bool);
	~DX11Renderer();

	bool initialise();
	void releaseObjects();
	void updateScene();
	void drawScene();
	int loop();
};

#endif
